<?php
/**
 * Extdb
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://code.google.com/p/extdb/wiki/NewBSDLicence
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Extdb
 * @package    Extdb
 * @copyright  Copyright (c) 2005-2008 ExtDb. (http://code.google.com/p/extdb/)
 * @license    http://code.google.com/p/extdb/wiki/NewBSDLicence New BSD License
 * @version    $Id: json.php 14 2009-07-29 21:37:40Z luke.richards.99 $
 */
 
require_once '../../config/bootstrap.php';

// create the request/response
$options = array('version' => '2.0');
$request = new Zend_Json_Server_Request_Http();
$request->setOptions($options);
$response = new Zend_Json_Server_Response_Http();
$response->setVersion($options['version']);

// create the server
$server = new Zend_Json_Server();
$server->setRequest($request);
$server->setResponse($response);

// work out the requested service
$serviceName = array_pop(explode('/', $_SERVER['REQUEST_URI']));
$class = Extdb_Webservice::getClassName($serviceName);
$server->setClass($class);

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    // Indicate the URL endpoint, and the JSON-RPC version used:
    $server->setTarget('/api/json')
           ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);

    // Grab the SMD
    $smd = $server->getServiceMap();

    // Return the SMD to the client
    header('Content-Type: application/json');
    echo $smd;
} else {
    $server->handle();
}
